package com.atguigu.algorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
class Edge{
    public int weight;
    public Node from;
    public Node to;
    public Edge(int weight,Node from,Node to){
        this.weight=weight;
        this.from=from;
        this.to=to;
    }
}
class Node{
    public int val;
    public int in; //入度
    public int out; //出度
    //对于无向图而言，入度==出度
    public ArrayList<Node> nexts; //发散出去的点集
    public ArrayList<Edge> edges; //边集
    public Node(int val){
        this.val=val;
        in=0;
        out=0;
        nexts=new ArrayList<>();
        edges=new ArrayList<Edge>();
    }
}
class Graph{
    public HashMap<Integer,Node> nodes;
    public HashSet<Edge> edges;
    public Graph(){
        nodes=new HashMap<>();
        edges=new HashSet<Edge>();
    }
}
public class graph {
    public static Graph createGraph(Integer[][] matrix){
        Graph graph=new Graph();
        for(int i=0;i<matrix.length;i++){
            Integer from=matrix[i][0];
            Integer to=matrix[i][1];
            Integer weight=matrix[i][2];
            if(!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            if(!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode=graph.nodes.get(from);
            Node toNode=graph.nodes.get(to);
            Edge newEdge=new Edge(weight,fromNode,toNode);
            fromNode.nexts.add(toNode);
            fromNode.out++;
            toNode.in++;
            fromNode.edges.add(newEdge);
            graph.edges.add(newEdge);
        }
        return graph;
    }
}
